﻿using QuanLySieuThi.Controllers;
using QuanLySieuThi.Models;
using QuanLySieuThi.Views;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLySieuThi
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();

            table.Columns.Add("MaHoaDon");
            table.Columns.Add("MaMatHang");
            table.Columns.Add("TenMatHang");
            table.Columns.Add("DonGia");
            table.Columns.Add("SoLuong");
            table.Columns.Add("ThanhTien");

        }
        public static NhanVien userLogin = null;
        private bool TimTheoMaKhachHang = true;
        private bool TimTheoMaMatHang = true;
        private DataTable table = new DataTable();


        private void frmMain_Load(object sender, EventArgs e)
        {

            frmLogin f = new frmLogin();
            if (f.ShowDialog() == DialogResult.OK)
            {

                if (this.Visible == false)
                    this.Visible = true;

                //  QuanLyKhachHang.UniqueObject.LoadData();
                //  QuanLyMatHang.UniqueObject.LoadData();
                SinhMaHoaDon();

                if (userLogin.MaChucVu.Equals("nhanvien"))
                {
                    nhanVienMenuTrip.Visible = false;
                    capnhatToolStripMenuItem.Visible = false;
                    menuGiaoDich.Visible = false;
                }
                else if (userLogin.MaChucVu.Equals("quanly"))
                {
                    nhanVienMenuTrip.Visible = true;
                    capnhatToolStripMenuItem.Visible = true;
                    menuGiaoDich.Visible = true;
                }
            }
            else
            {
                this.Close();
            }
        }




        private void thoátToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            userLogin = null;
            this.Visible = false;
            frmMain_Load(sender, e);
        }

        private void hoàngHóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmHangHoa f = new frmHangHoa();
            f.ShowDialog();
        }

  

        private void nhanVienMenuTrip_Click(object sender, EventArgs e)
        {
            frmNhanVien f = new frmNhanVien();
            f.ShowDialog();
            //f.Show();
        }


        private void click_Them(object sender, EventArgs e)
        {
            //Thêm hàng vào danh sách
            //try
            //{
                QuanLyMatHang auq;
                //Kiểm tra số lượng mua có vượt quá số lượng hàng hiện tại ko
                int soLuongHienCo = QuanLyMatHang.UniqueObject.SoLuongHienCo(txtMaMH.Text);
                if (int.Parse(txtSL.Text) > soLuongHienCo)
                {
                    MessageBox.Show("Số lượng nhập quá quy định!");
                    return;
                }

                int tong = int.Parse(txtSL.Text) * int.Parse(txtDonGia.Text);
                table.Rows.Add(txtMaHoaDon.Text, txtMaMH.Text, txtTenMH.Text, txtDonGia.Text, txtSL.Text, tong.ToString());
                int tong2 = int.Parse(txtTongTien.Text) + tong;
                txtTongTien.Text = tong2.ToString();
                dataGridView1.DataSource = table;

                foreach (var item in groupBox2.Controls)
                {
                    TextBox tb = item as TextBox;
                    if (tb != null)
                        tb.Text = "";
                }
            //}
            //catch (Exception ex)
            //{

            //    MessageBox.Show(ex.Message);
            //}
        }

        private void click_ThanhToan(object sender, EventArgs e)
        {
            try
            {
                //Thêm Hóa đơn 
                HoaDonXuat hd = new HoaDonXuat();
                hd.MaHoaDonXuat = txtMaHoaDon.Text;
                hd.MaNhanVien = userLogin.MaNhanVien;
                hd.ThoiGian = DateTime.Now.Date;
                hd.TongTien = int.Parse(txtTongTien.Text);
                QuanLyHoaDonXuat.UniqueObject.Add(hd);

                //sau đó thêm các chi tiết hóa đơn

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    HoaDonChiTietXuat ct = new HoaDonChiTietXuat();
                    ct.MaHoaDonXuat = txtMaHoaDon.Text;
                    ct.MaMatHang = table.Rows[i]["MaMatHang"].ToString();
                    ct.SoLuong = int.Parse(table.Rows[i]["SoLuong"].ToString());
                    ct.GiaBan = int.Parse(table.Rows[i]["DonGia"].ToString());
                    QuanLyHoaDonChiTietXuat.UniqueObject.Add(ct);
                }


                MessageBox.Show("OK");
                SinhMaHoaDon();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void txtMaMH_TextChanged(object sender, EventArgs e)
        {
            if (TimTheoMaMatHang)
            {
                MatHang m = QuanLyMatHang.UniqueObject.LayTheoMa(txtMaMH.Text);
                if (m != null)
                {
                    txtTenMH.Text = m.TenMatHang;
                    txtDonGia.Text = m.GiaBan.ToString();
                }
                else
                {
                    txtTenMH.Text = "";
                    txtDonGia.Text = "";
                }
            }
        }

        private void txtTenMH_TextChanged(object sender, EventArgs e)
        {
            //if (!TimTheoMaMatHang)
            //{
            //    MatHang m = QuanLyMatHang.UniqueObject.LayTheoTen(txtTenMH.Text);
            //    if (m != null)
            //    {
            //        txtMaMH.Text = m.MaMatHang;
            //        txtDonGia.Text = m.GiaBan.ToString();
            //    }
            //    else
            //    {
            //        txtMaMH.Text = "";
            //        txtDonGia.Text = "";
            //    }
            //}
        }

        private void button3_Click(object sender, EventArgs e)
        {

            SinhMaHoaDon();

        }



        private void txtMaKH_KeyPress(object sender, KeyPressEventArgs e)
        {
            TimTheoMaKhachHang = true;
        }

        private void txtTenKH_KeyPress(object sender, KeyPressEventArgs e)
        {
            TimTheoMaKhachHang = false;
        }

        private void txtMaMH_KeyPress(object sender, KeyPressEventArgs e)
        {
            TimTheoMaMatHang = true;
        }

        private void txtTenMH_KeyPress(object sender, KeyPressEventArgs e)
        {
            TimTheoMaMatHang = false;
        }

        private void SinhMaHoaDon()
        {
         

            foreach (var item in groupBox2.Controls)
            {
                TextBox tb = item as TextBox;
                if (tb != null)
                    tb.Text = "";
            }

            dataGridView1.DataSource = null;
            txtTongTien.Text = "0";
            table.Rows.Clear();
            string s = string.Format("{0}{1}{2}{3}", DateTime.Now.DayOfYear, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Millisecond);
            txtMaHoaDon.Text = s;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void kiểmKêToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmKiemKe f = new frmKiemKe();
            f.ShowDialog();
            //f.Show();
        }

        private void cậpNhậtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmHangHoa f = new frmHangHoa();
            f.ShowDialog();
            //f.Show();
        }

        private void nhaphangToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmNhapHang f = new frmNhapHang();
            f.ShowDialog();
            // f.Show();
        }

        private void nhàCungCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmNhaCungCap f = new frmNhaCungCap();
            f.ShowDialog();
        }

        private void nhậpHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmNhapHang f = new frmNhapHang();
            f.ShowDialog();
        }

      
    }
}
